<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\BaseController;
use app\common\lib\Show;
use think\Exception;
use think\facade\View;
use think\facade\Request;
use app\common\business\Category as BusiCate;
use app\common\lib\StatusLib;

class Category extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $data   = ['pid' => Request::param('pid', 0, 'intval')];

        try{
            $object = new BusiCate();
            $category = $object->getLists($data);
        } catch (\Exception $e) {
            $category = [];
        }

        return View::fetch('', [
            'category' => $category,
            'deleteStatus' => config('status.mysql.table_delete')
        ]);
    }

    public function getChildCate()
    {
        $data   = ['pid' => Request::param('pid', 0, 'intval')];

        try{
            $object = new BusiCate();
            $category = $object->getLists($data);
        } catch (\Exception $e) {
            $category = [];
        }

        return Show::success($category);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function add()
    {
        $pid = Request::param('id', 0, 'intval');

        try{
            $busiCate = new BusiCate();
            $cateData = $busiCate->getNormalCate();
        } catch (\Exception $e) {
            $cateData = [];
        }

        return View::fetch('', [
            'catedata' => json_encode($cateData),
            'pid'      => $pid
        ]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        // 判断数据提交方式
        if (!Request::isPost()) {
            return Show::error('非法请求', config('status.request_action_error'));
        }

        // 获取提交数据
        $data = [
            'name' => Request::param('name', '', 'trim'),
            'pid'  => Request::param('pid', 0, 'intval')
        ];

        // 提交数据验证
        $validate = new \app\admin\validate\Category();
        if (!$validate->scene('save')->check($data)) {
            return Show::error($validate->getError());
        }

        try{
            $busiCate = new BusiCate();
            $result   = $busiCate->add($data);
        } catch (\Exception $e) {
            return Show::error($e->getMessage());
        }

        return $result ? Show::success() : Show::error('添加分类失败');
    }

    /**
     * 分类排序修改
     *
     * @param Request $request
     * @return \think\response\Json
     */
    public function listorder(Request $request)
    {
        // 获取提交数据
        $data = [
            'id'        => Request::param('id', 0, 'intval'),
            'listorder' => Request::param('sort', 0, 'intval')
        ];

        $validate = new \app\admin\validate\Category();
        if (!$validate->scene('listorder')->check($data)) {
            return Show::error($validate->getError());
        }

        try{
            $busiCate = new BusiCate();
            $res      = $busiCate->updateListorderById($data['id'], $data['listorder']);
        } catch (\Exception $e) {
            return Show::error($e->getMessage());
        }

        return $res ? Show::success([], '修改成功') : Show::error('修改失败');
    }

    /**
     * 修改分类状态
     *
     * @param Request $request
     * @return \think\response\Json
     * @throws Exception
     */
    public function status(Request $request)
    {
        // 获取当前提交数据
        $data = [
            'status' => Request::param('status', 0, 'intval'),
            'id' => Request::param('id', 0, 'intval')
        ];

        // 判断 id 和 状态是否正确
        if (!$data['id'] || !in_array($data['status'], StatusLib::getTableStatus())) return Show::error('参数错误');

        try {
            $busiCate = new BusiCate();
            $res = $busiCate->updateStatusById($data['id'], $data['status']);
        } catch (\Exception $e) {
            return Show::error($e->getMessage());
        }

        return $res ? Show::success() : Show::error('状态修改失败');
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read(int $id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(int $id)
    {
        $busiCate = new BusiCate();
        try{
            $data     = $busiCate->getById($id);
        } catch (\Exception $e) {
            $data     = [];
        }
        try{
            $cateData = $busiCate->getNormalCate();
        } catch (\Exception $e) {
            $cateData = [];
        }

        return View::fetch('', ['id' => $id, 'data' => $data, 'catedata' => json_encode($cateData)]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(int $id)
    {
        // 检查当前请求方式
        if (!Request::isPost()) {
            return Show::error('非法请求');
        }

        // 获取当前请求数据
        $data = [
            'pid'  => Request::param('pid', 0, 'intval'),
            'name' => Request::param('name', '', 'trim')
        ];

        // 请求数据验证
        $validate = new \app\admin\validate\Category();
        if (!$validate->scene('save')->check($data)) {
            return Show::error($validate->getError());
        }

        // 修改数据
        try {
            $busiCate = new BusiCate();
            $res      = $busiCate->updateCategoryById($id, $data);
        } catch (\Exception $e) {
            return Show($e->getMessage());
        }

        return $res ? Show::success() : Show::error('分类修改失败');
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    public function dialog()
    {
        $busiCate = new BusiCate();
        $category = $busiCate->getNormalByPid();
        return View::fetch('', ['category' => json_encode($category)]);
    }

    public function getByPid()
    {
        $pid = Request::param('pid', 0, 'intval');
        $busiCate = new BusiCate();
        $result   = $busiCate->getNormalByPid($pid);
        return Show::success($result);
    }
}
